/**
 * 
 */
package com.ls.lserp.modules.produce.service;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.modules.order.dao.OrderDao;
import com.ls.lserp.modules.order.entity.CustomizationContract;
import com.ls.lserp.modules.order.entity.StyleOperationtings;
import com.ls.lserp.modules.produce.dao.ProductionLocationDao;
import com.ls.lserp.modules.produce.entity.ProduceParameter;
import com.ls.lserp.modules.produce.entity.ProductionLocation;
import com.ls.lserp.modules.setting.entity.BarcodeMan;




@Service
@Transactional(readOnly = true)
public class ProductionLocationService extends CrudService<ProductionLocationDao, ProductionLocation> {
    @Autowired
    private ProductionLocationDao productionLocationDao;
    @Autowired
    private OrderDao orderDao;
    
    public Page<ProductionLocation> findList(Page<ProductionLocation> page, ProductionLocation productionLocation) {
		 // 设置分页参数
    	productionLocation.setPage(page);
	       // 执行分页查询
	       page.setList(productionLocationDao.findList(productionLocation));
	       
	       return page;
		
	}
    
    /**
	  * @Title: saveProduce
	  * @Description: 保存基准数据
	  * @param produceparameter
	  * @return void 
	  */
	@Transactional(readOnly = false)
	public void saveproductionLocation(ProductionLocation productionLocation) {
		// 判断新建编辑
		//若为null 执行插入语句（新建）
		if (productionLocation.getId()==null){
			productionLocation.preInsert();
			productionLocationDao.insert(productionLocation);
			// 工序实体集
			List<StyleOperationtings> sList = new ArrayList<>();
			CustomizationContract customizationContract = new CustomizationContract();
			// 设置 合同状态 为 生产中
			customizationContract.setOrderStatus(ResourcesUtil.getConfig("Workshop_30"));
			// 所有合同ID集合
			List<CustomizationContract> contractIdList = orderDao.findContractIdList(customizationContract);
			if (contractIdList != null && contractIdList.size() > 0) {
				for(CustomizationContract contract : contractIdList){
					// 根据合同ID获取订单工序数据
					sList = orderDao.findOperatingData(contract);
					if (sList != null && sList.size() > 0) {
						for(StyleOperationtings temp : sList){
							// 设置新工序ID
							temp.setOperationItmeId(productionLocation.getId());
							// 设置更新状态
							temp.setOperatingStatus(ResourcesUtil.getConfig("OPERATING_STATUS00"));
						}
						// 插入工序数据
						orderDao.insertOperatingList(sList);
					}
				}
			}
		// 编辑
		}else{
			// 更新数据
			productionLocation.preUpdate();
			productionLocationDao.update(productionLocation);
			//productionLocationDao.deleteR(productionLocation);
		}
	}
    
    /**
	  * @Title: deleteProduce
	  * @Description: 删除基准数据(逻辑删除)
	  * @param produceparameter
	  * @return void 
	  */
	
	@Transactional(readOnly = false)
	public void deleteProduce(ProductionLocation productionLocation) {
		productionLocationDao.delete(productionLocation);
		// 逻辑删除款式工序表数据
		orderDao.deleteOperatingDataByID(productionLocation.getId());
	}
    


	
	
	
}